package www.admin.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import www.admin.dao.IBorrowMoneyDao;
import www.admin.model.BorrowMoney;
import www.admin.mybatis.Criteria;
import www.admin.service.IBorrowMoneyService;
import www.admin.utils.DictConstant;
import www.admin.utils.Layui;
import www.admin.utils.MessageConstant;
import www.admin.utils.TranslateToMap;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 
 * BorrowMoney
 * 
 **/

@Service
public class BorrowMoneyServiceImpl implements IBorrowMoneyService{


	public Layui queryPage (Criteria<BorrowMoney> param){
		PageHelper.startPage(param.getStartIndex(), param.getPageSize());
		List<BorrowMoney> list = borrowMoneyRepository.queryPage(param);
			PageInfo<BorrowMoney> paInfo = new PageInfo<BorrowMoney>(list);
	return Layui.data((int)paInfo.getTotal(), paInfo.getList()); }

	public Integer queryPageCount (Criteria<BorrowMoney> param){
		return (Integer) borrowMoneyRepository.queryPageCount(param);
	}

	public BorrowMoney  queryEntity (String id ){
		return borrowMoneyRepository.queryEntity(id);
	}

	public String deleteById (String id ){
		int result = borrowMoneyRepository.deleteById(id);
			if ( result != 1) {
					return MessageConstant.USER_TYPE_3;
			}
			return null;
	}

	public String insert(BorrowMoney record ){
		int result = borrowMoneyRepository.insert(record);
			if ( result != 1) { 
					return MessageConstant.USER_TYPE_4;
			}
			return null;
	}

	public String update (Criteria<BorrowMoney> param){
		int result = borrowMoneyRepository.update(param);
			if ( result != 1) { 
					return MessageConstant.USER_TYPE_5;
			}
			return null;
	}

	@Override
	public List<Map<String, Object>> queryPageList(Criteria<BorrowMoney> param) {
		List<BorrowMoney> list = borrowMoneyRepository.queryPage(param);
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getStatus() == 1) {
				list.get(i).setStatusZh(DictConstant.BORROW_TYPE_1);
			}else if (list.get(i).getStatus() == 2) {
				list.get(i).setStatusZh(DictConstant.BORROW_TYPE_2);
			}else if (list.get(i).getStatus() == 3) {
				list.get(i).setStatusZh(DictConstant.BORROW_TYPE_3);
			}else if (list.get(i).getStatus() == 4) {
				list.get(i).setStatusZh(DictConstant.BORROW_TYPE_4);
			}else if (list.get(i).getStatus() == 5) {
				list.get(i).setStatusZh(DictConstant.BORROW_TYPE_5);
			}
		}
		return TranslateToMap.convertListBean2ListMap(list,BorrowMoney.class);
	}
	
	@Override
	public BorrowMoney queryHomeMoney(Criteria<BorrowMoney> param) {
		BorrowMoney money = borrowMoneyRepository.queryHomeMoney(param);
        if (money != null && money.getSumMoney()!= null) {
        	if (money.getWdqMoney()!= null) {
        		money.setWdqPercentum(money.getWdqMoney().divide(money.getSumMoney(), 2 ,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
			}
        	if (money.getYjqMoney()!= null) {
        		money.setYjqPercentum(money.getYjqMoney().divide(money.getSumMoney(), 2 ,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
        	}
        	if (money.getYqMoney()!= null) {
        		money.setYqPercentum(money.getYqMoney().divide(money.getSumMoney(), 2 ,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
        	}
        	if (money.getShMoney()!= null) {
        		money.setPercentum(money.getShMoney().divide(money.getSumMoney(), 2 ,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
        	}
		}
		return money;
	}

	@Override
	public List<BorrowMoney> queryPageListKey(Criteria<BorrowMoney> param) {
		return borrowMoneyRepository.queryPage(param);
	}
	
	@Resource
	private IBorrowMoneyDao borrowMoneyRepository;

}